package com.wutongyu.mannyburger;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * 购物车对话框适配器，用于在对话框中显示购物车商品列表
 */
public class CartDialogAdapter extends RecyclerView.Adapter<CartDialogAdapter.CartItemViewHolder> {

    private final Context context;
    private final List<CartItem> cartItems;
    private final CartManager cartManager;
    private final CartDialogListener listener;
    private Animation quantityChangeAnimation;

    /**
     * 购物车对话框事件监听接口
     */
    public interface CartDialogListener {
        void onCartUpdated(List<CartItem> cartItems, double totalPrice);
    }

    /**
     * 构造函数
     * @param context 上下文
     * @param cartItems 购物车商品列表
     * @param listener 事件监听器
     */
    public CartDialogAdapter(Context context, List<CartItem> cartItems, CartDialogListener listener) {
        this.context = context;
        this.cartItems = cartItems;
        this.cartManager = CartManager.getInstance();
        this.listener = listener;
        this.quantityChangeAnimation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
    }

    @NonNull
    @Override
    public CartItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_cart_item, parent, false);
        return new CartItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CartItemViewHolder holder, int position) {
        CartItem cartItem = cartItems.get(position);
        Product product = cartItem.getProduct();
        
        if (product != null) {
            // 设置商品信息
            holder.nameTextView.setText(product.getName());
            holder.priceTextView.setText("¥" + String.format("%.2f", product.getPrice() * cartItem.getQuantity()));
            holder.quantityTextView.setText(String.valueOf(cartItem.getQuantity()));
            
            // 设置商品图片（如果有）
            if (product.getImageResId() != 0) {
                holder.imageView.setImageResource(product.getImageResId());
            } else {
                holder.imageView.setImageResource(R.drawable.bg); // 默认图片
            }
            
            // 增加按钮点击事件
            holder.btnIncrease.setOnClickListener(v -> {
                cartManager.addProduct(product);
                notifyDataSetChanged();
                if (listener != null) {
                    listener.onCartUpdated(cartManager.getCartItems(), cartManager.getTotalPrice());
                }
            });
            
            // 减少按钮点击事件
            holder.btnDecrease.setOnClickListener(v -> {
                cartManager.removeProduct(product);
                if (cartItem.getQuantity() <= 1) {
                    // 如果商品数量减为0，将从列表中移除，需要通知适配器
                    notifyDataSetChanged();
                } else {
                    holder.quantityTextView.setText(String.valueOf(cartItem.getQuantity() - 1));
                    holder.quantityTextView.startAnimation(quantityChangeAnimation);
                    holder.priceTextView.setText("¥" + String.format("%.2f", product.getPrice() * (cartItem.getQuantity() - 1)));
                }
                if (listener != null) {
                    listener.onCartUpdated(cartManager.getCartItems(), cartManager.getTotalPrice());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return cartItems.size();
    }

    /**
     * 更新购物车数据
     */
    public void updateCartItems(List<CartItem> newCartItems) {
        this.cartItems.clear();
        this.cartItems.addAll(newCartItems);
        notifyDataSetChanged();
    }

    /**
     * 购物车商品视图持有者
     */
    static class CartItemViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView nameTextView;
        TextView priceTextView;
        TextView quantityTextView;
        ImageButton btnDecrease;
        ImageButton btnIncrease;

        public CartItemViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.cart_item_image);
            nameTextView = itemView.findViewById(R.id.cart_item_name);
            priceTextView = itemView.findViewById(R.id.cart_item_price);
            quantityTextView = itemView.findViewById(R.id.cart_item_quantity);
            btnDecrease = itemView.findViewById(R.id.cart_item_btn_decrease);
            btnIncrease = itemView.findViewById(R.id.cart_item_btn_increase);
        }
    }
} 